%{

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "cue_lexer_parser.h"

#define YY_INPUT(buf,res,max_size)						\
do {										\
	if (yyextra->pos == yyextra->bufsize) {					\
		res = YY_NULL;							\
	} else {								\
		res = yyextra->bufsize - yyextra->pos;				\
		res > max_size ? res = max_size : 0;				\
		memcpy(buf, yyextra->buffer + yyextra->pos, res);		\
		yyextra->pos += res;						\
										\
		yyextra->pos == yyextra->bufsize ? res = YY_NULL : 0;		\
	}									\
} while (0)

%}

%option bison-bridge noyywrap debug nodefault reentrant case-insensitive

BLANK		[ \t]
STARTBLANK	^{BLANK}*
DOUBLEDIGIT	[0-9]{2}

%s ARGS REM_STRING
%x QUOTED_STR

%%

{STARTBLANK}CATALOG		{ BEGIN(ARGS); return CATALOG; }
{STARTBLANK}CDTEXTFILE		{ BEGIN(ARGS); return CDTEXTFILE; }
{STARTBLANK}FILE		{ BEGIN(ARGS); return FILETOKEN; }
{STARTBLANK}FLAGS		{ BEGIN(ARGS); return FLAGS; }
{STARTBLANK}INDEX		{ BEGIN(ARGS); return INDEX; }
{STARTBLANK}ISRC		{ BEGIN(ARGS); return ISRC; }
{STARTBLANK}PERFORMER		{ BEGIN(ARGS); return PERFORMER; }
{STARTBLANK}POSTGAP		{ BEGIN(ARGS); return POSTGAP; }
{STARTBLANK}PREGAP		{ BEGIN(ARGS); return PREGAP; }
{STARTBLANK}REM			{ BEGIN(REM_STRING); return REM; }
{STARTBLANK}SONGWRITER		{ BEGIN(ARGS); return SONGWRITER; }
{STARTBLANK}TITLE		{ BEGIN(ARGS); return TITLE; }
{STARTBLANK}TRACK		{ BEGIN(ARGS); return TRACK; }

<REM_STRING>.*$		{ BEGIN(INITIAL); yylval->sval = strdup(yytext); return STRING; }

<ARGS>{
	{DOUBLEDIGIT}:{DOUBLEDIGIT}:{DOUBLEDIGIT} {
			yytext[2] = yytext[5] = '\0';
			yylval->timeval.mins = atoi(yytext);
			yylval->timeval.secs = atoi(yytext + 3);
			yylval->timeval.frames = atoi(yytext + 5);
			return CDTIME;
			}

	[0-9]{13}	{ yylval->sval = strdup(yytext); return MCN; }
	[0-9]+		{ yylval->ival = atoi(yytext); return INTEGER; }
	\"		{ BEGIN(QUOTED_STR); }

	BINARY		return BINARY;
	MOTOROLA	return MOTOROLA;
	AIFF		return AIFF;
	WAVE		return WAVE;
	MP3		return MP3;
	
	DCP		return DCP;
	4CH		return FOURCH;
	PRE		return PRE;
	SCMS		return SCMS;

	AUDIO		return AUDIO;
	CDG		return CDG;
	MODE1/2048	return MODE1_2048;
	MODE1/2352	return MODE1_2352;
	MODE2/2336	return MODE2_2336;
	MODE2/2352	return MODE2_2352;
	CDI/2336	return CDI_2336;
	CDI/2352	return CDI_2352;
};

<QUOTED_STR>{
	[^\"\n]*|\\\"	{ yymore(); }
	\"		{ BEGIN(ARGS); yylval->sval = strdup(yytext); yylval->sval[yyleng - 1] = '\0'; return STRING; }
}

{BLANK}*
(\r|\n)*		BEGIN(INITIAL);

<<EOF>>			{ yyterminate();}

%%
/*
int
main(int argc, char **argv) {
	printf("starting...\n");

	YYSTYPE val;
	yyscan_t scanner;
	yylex_init(&scanner);
	yyset_debug(1, scanner);
	for (; yylex(&val, scanner) != 0;) {}
	yylex_destroy(scanner);

	return 0;
}
*/

